const { override, fixBabelImports, addWebpackExternals, addWebpackPlugin,overrideDevServer } = require('customize-cra');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const addCustomize = () => config => {
  if (process.env.NODE_ENV === 'production') {
    // 关闭sourceMap
    config.devtool = false;
    // 配置打包后的文件位置
    // config.output.path = __dirname + '/dist/';
    // config.output.publicPath = '/user-meeting/'; //package.json中homepage配置
    // 添加js打包gzip配置
    config.plugins.push(
      new CompressionWebpackPlugin({
        test: /\.js$|\.css$/,
        threshold: 1024,
      }),
    )
  }
  return config;
}

// 跨域配置
const devServerConfig = () => config => {
  return {
    ...config,
    // 服务开启gzip
    compress: true,
    proxy: {
      '/api/v1': {
        target: 'http://localhost:3005',
        changeOrigin: true,
        pathRewrite: {
          '^/api/v1': '',
        },
      }
    }
  }
}

module.exports = {
  webpack: override(
		// antd按需加载
    // fixBabelImports('antd', {
    //   libraryName: 'antd',
    //   libraryDirectory: 'es',
    //   style: true,
    // }),
    addCustomize(),
    addWebpackPlugin(
      new BundleAnalyzerPlugin({
        analyzerMode: 'static', //输出静态报告文件report.html，而不是启动一个web服务
      })
    ),
    //别名
    // addWebpackAlias({
    //   ["ag-grid-react$"]: path.resolve(__dirname, "src/shared/agGridWrapper.js")
    // }),
  ),
  devServer: overrideDevServer(
    devServerConfig()
  )
}

